﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;

namespace MyUtility
{
    public partial class WhereAmIPage : PhoneApplicationPage
    {
        public WhereAmIPage()
        {
            InitializeComponent();
        }
		
		GeoCoordinateWatcher gps = new GeoCoordinateWatcher();
        private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			//16.4341,102.8361
			myMap.Center = new GeoCoordinate(16.4341,102.8361);
			myMap.Mode = new AerialMode();
			//myMap.Mode = new RoadMode();
			myMap.ZoomLevel = 18.0;
			gps.PositionChanged +=new System.EventHandler<System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate>>(gps_PositionChanged);
        	gps.Start();
		}
        private void gps_PositionChanged(object sender, System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate> e)
        {
        	// TODO: Add event handler implementation here.
			if(gps.Status == GeoPositionStatus.Ready){
				myMap.Center = e.Position.Location;
			}
        }
		public GeoCoordinate getCurrentLocation(){
			GeoCoordinate result = null;
			if(gps.Status == GeoPositionStatus.Ready){
				result = gps.Position.Location;
			}else if(gps.Status == GeoPositionStatus.NoData){
				result = new GeoCoordinate(0.0,0.0);
			}else if(gps.Status == GeoPositionStatus.Disabled){
				result = null;
			}
			return result;
		}
    }
}
